import { Injectable, Inject } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import { Item } from './interfaces/item.interface';
import { ItemSchemas } from './schemas/item.schemas';

@Injectable()
export class TestService {
  constructor(
    // 选择snake表
    // @InjectModel('snake')
    @Inject('ITEM_MODEL')
    private readonly itemModel: Model<Item>
  ) {}

  /**
   * 查询所有
   */
  async findAll(): Promise<Item[]> {
    return await this.itemModel.find();
  }

  /**
   * 查询单个信息
   * @param id
   */
  async findOne(id: string): Promise<Item> {
    return await this.itemModel.findOne({ _id: id });
  }

  /**
   * 新建元素
   * @param item
   */
  async createOne(item: Item): Promise<Item> {
    const newItem = new this.itemModel(item);
    return await newItem.save();
  }

  /**
   * 更新数据
   * @param id 主键
   * @param item
   */
  async updateOne(id: string, item: Item): Promise<Item> {
    return await this.itemModel.findByIdAndUpdate(id, item, { new: true });
  }

  /**
   *
   * @param id 删除元素
   */
  async deleteOne(id: string): Promise<Item> {
    return await this.itemModel.findByIdAndRemove(id);
  }
}
